package cn.laixueit;

import cn.laixueit.mapper.StudentMapper;
import cn.laixueit.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class TestMyBatis {
    public static void main(String[] args) throws IOException {
        // 加载核心配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");

        // 得到会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        // 打开一个会话

        SqlSession session = sqlSessionFactory.openSession(true);

//        List<Object> list = session.selectList("test.findall");

//        System.out.println(list);

//        Student one = session.selectOne("test.findbyid");

//        System.out.println(one);

//        Object o = session.selectOne("test.findbyid", 2);
//        System.out.println(o);


        StudentMapper mapper = session.getMapper(StudentMapper.class);
//        System.out.println(mapper.findall());
//        System.out.println(mapper.findbyid(2));

//        Student student = new Student();
//        student.setUsername("建国");
//        student.setAddress("武汉");
//        mapper.save(student);
//        int i = mapper.delbyid(5);

//        Student student = new Student();
//        student.setId(4);
//        student.setUsername("建党");

//      student.setAddress("中华人民共和国");

//        int i = mapper.update(student);



//        System.out.println(i);


//        Student student = new Student();
//        student.setUsername("%三%");
//        List<Student> studentList = mapper.findListName(student);
//        studentList.forEach(System.out::println);

//        int pagesize= 2;
//
//        Page page = new Page();
//        page.setStartPage((2-1)*pagesize);// 0  1,2
//        page.setPageSize(pagesize);
//        List<Student> studentList = mapper.findPage(page);


//        HashMap<String, Integer> map = new HashMap<>();
//        map.put("startPage",2);
//        map.put("pageSize",2);
//        List<Student> studentList = mapper.findPage2(map);



//        Student student = new Student();
//        student.setUsername("三");
//        List<Student> studentList = mapper.findListName2(student);
//        studentList.forEach(System.out::println);


//        System.out.println(mapper.findbyid(6));



//        List<Student> studentList = mapper.findListName4("三");


        HashMap<String, Object> map = new HashMap<>();


        Student student = new Student();
        student.setUsername("三");
        student.setAddress("武汉");
        map.put("student",student);

        map.put("startPage",0);
        map.put("pageSize",2);

        List<Student> studentList = mapper.findPageCondition(map);
        studentList.forEach(System.out::println);

    }
}
